﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace CRM
{
    public partial class ServiceEdit : System.Web.UI.Page
    {
        protected override void OnPreLoad(EventArgs e)
        {
            Common.CheckMes.CheckState("User_ID", "Index.aspx", "用户登录信息丢失，请重新登录...");
            string PowerMes = Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("Power_Mes"), 6);

            if (PowerMes.IndexOf("2_2") < 0)
            {
                Common.MessageBox.Show(Page, "你没有编辑的权限...", "ServiceInfo.aspx", "BACK");
            }
            base.OnPreLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPower();
                LoadData();
            }
        }

        private void LoadData()
        {
            string strSID = Common.GetMes.GetRequestQuery(Request, "SID");
            if (!string.IsNullOrEmpty(strSID))
            {
                SqlDataReader DataRead = BLL.ServiceUser.GetAllServiceUserInfo(strSID);
                if (DataRead.HasRows)
                {
                    DataRead.Read();
                    txtAdress.Value = DataRead["T_ServiceAdress"].ToString();
                    txtBirthDay.Value = DataRead["T_ServicebirthDay"].ToString();
                    txtMail.Value = DataRead["T_ServiceMail"].ToString();
                    txtQQ.Value = DataRead["T_ServiceQQ"].ToString();
                    txtMoblie.Value = DataRead["T_ServiceTel"].ToString();
                    txtPerson.Value = DataRead["T_ServiceName"].ToString();
                    DDLPower.SelectedValue = DataRead["T_PowerID"].ToString();
                    DDLState.SelectedValue = DataRead["T_ServiceState"].ToString();
                    txtRemark.Value = DataRead["T_Remark"].ToString();
                    txtPassWord.Attributes.Add("value", "******");
                    txtCheckPass.Attributes.Add("value", "******");
                    txtPassWord.Enabled=txtCheckPass.Enabled = false;
                }
                DataRead.Close();
                DataRead.Dispose();
            }
        }

        private void LoadPower()
        {
            DDLPower.DataSource = BLL.ServiceUser.GetPowerInfo("");
            DDLPower.DataTextField = "T_PowerName";
            DDLPower.DataValueField = "T_PowerID";
            DDLPower.DataBind();
        }

        protected void BtnSaveUserInfo_Click(object sender, EventArgs e)
        {
            //构建用户实体对象
            Model.T_ServiceUser ServiceModel = new CRM.Model.T_ServiceUser();
            //从用户界面中获取用户输入的信息，赋给实体属性
            ServiceModel.T_PowerID = Convert.ToInt32(DDLPower.SelectedValue);
            ServiceModel.T_ServiceAdress = txtAdress.Value.Trim();
            ServiceModel.T_Remark = txtRemark.Value.Trim();
            ServiceModel.T_ServicebirthDay = txtBirthDay.Value.Trim();
            ServiceModel.T_ServiceMail = txtMail.Value.Trim();
            ServiceModel.T_ServiceName = txtPerson.Value.Trim();
            //对于密码，使用MD5加密方式进行加密
            ServiceModel.T_ServicePass = (!string.IsNullOrEmpty
                (Common.GetMes.GetRequestQuery(Request, "SID"))) ? 
                "" : Common.EncryptString.EncryptMD5(txtPassWord.Text.Trim());
            ServiceModel.T_ServiceQQ = txtQQ.Value.Trim();
            ServiceModel.T_ServiceTel = txtMoblie.Value.Trim();
            ServiceModel.T_ServiceState = 
                Convert.ToInt32(DDLState.SelectedValue);
            ServiceModel.T_ServiceID = (string.IsNullOrEmpty
                (Common.GetMes.GetRequestQuery(Request, "SID"))) ? 
                "" : Common.GetMes.GetRequestQuery(Request, "SID").ToString();
           //调用存储过程更新用户信息
            if (BLL.ServiceUser.EditServiceUserInfo(ServiceModel))
            {   //如果为添加新的用户，即T_ServiceID为空，查询字符串未包含值
                if (string.IsNullOrEmpty(ServiceModel.T_ServiceID))
                {   //显示插入提示消息，提示是否再次进行插入
                    Common.MessageBox.myScriptMes(Page, 
                        "if(confirm(\"信息保存成功，是否继续添加？否则返回信息列表。\")) "+
                        "{location.href=\"ServiceEdit.aspx\"} else "+
                        "{location.href=\"ServiceInfo.aspx\"} ", "Suess");
                }
                else
                {   //提示修改成功，并进行页面重定向
                    Common.MessageBox.Show(Page, "信息修改成功......", 
                        "ServiceInfo.aspx", "Suess2");
                }
            }
            else
            {  //提示信息保存失败
                Common.MessageBox.Show(Page,
                    "信息保存失败，青重试或联系技术人员......", 
                    "ServiceEdit.aspx", "Error");

            }
        }
    }
}
